<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:scoringMetrics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:scoringMetrics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:scoringMetrics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:scoringMetrics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scoringMetricsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="评估指标id" align="center" prop="scoringMetricsId" v-if="true"/>-->
      <el-table-column label="评估指标名称" align="center" prop="scoringMetricsName" />
      <el-table-column label="评估指标年份" align="center" prop="scoringMetricsYear" />
      <el-table-column label="评估指标" prop="scoringMetricsArray" width="300px" >
        <template slot-scope="scope">
          <div v-for="item in scope.row.scoringMetricsArray">
            <el-tag
              style="margin-top: 5px"
              :key="item"
              effect="dark">
              指标名称：{{ item.metricsName }} ，指标分数：{{ item.metricsScore }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="直接答辩分数线" align="center" prop="directReplyScore" />
      <el-table-column label="修改后答辩分数线" align="center" prop="reviseReplyScore" />
      <el-table-column label="重审后答辩分数线" align="center" prop="reviseAuditScore" />
      <el-table-column label="不同意答辩" align="center" prop="disagreeReplyScore" />
      <el-table-column label="开启的指标" align="center" prop="defaultState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.defaultState"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:scoringMetrics:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:scoringMetrics:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评估指标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="评估指标名称" prop="scoringMetricsName">
          <el-input v-model="form.scoringMetricsName" placeholder="请输入评估指标名称" />
        </el-form-item>
        <el-form-item label="评估指标年份" prop="scoringMetricsYear">
          <el-input v-model="form.scoringMetricsYear" placeholder="请输入评估指标年份" />
        </el-form-item>
        <el-form-item label="评估指标" prop="scoringMetricsArray">
          <el-table
            :data="form.scoringMetricsArray"
            stripe
            style="margin-top:20px; width: 100%">
            <el-table-column
              prop="metricsName"
              label="指标名称"
              width="220">
              <template slot-scope="scope">
                <el-input v-model="scope.row.metricsName"></el-input>
              </template>
            </el-table-column>
            <el-table-column
              prop="metricsScore"
              label="指标分数"
              width="220">
              <template slot-scope="scope">
                <el-input type="number" v-model="scope.row.metricsScore"></el-input>
              </template>
            </el-table-column>
            <el-table-column
              prop="operate"
              label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="handleDeleteExperience(scope.$index, scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddExperienceline">新增评估指标
            </el-button>
          </div>
        </el-form-item>
        <el-form-item label="直接答辩分数线" prop="directReplyScore">
          <el-input v-model="form.directReplyScore" placeholder="请输入直接答辩分数线" />
        </el-form-item>
        <el-form-item label="修改后答辩分数线" prop="reviseReplyScore">
          <el-input v-model="form.reviseReplyScore" placeholder="请输入修改后答辩分数线" />
        </el-form-item>
        <el-form-item label="重审后答辩分数线" prop="reviseAuditScore">
          <el-input v-model="form.reviseAuditScore" placeholder="请输入修改后审核答辩分数线" />
        </el-form-item>
        <el-form-item label="不同意答辩" prop="disagreeReplyScore">
          <el-input v-model="form.disagreeReplyScore" placeholder="请输入不同意答辩" />
        </el-form-item>
        <el-form-item label="开启的指标" prop="defaultState">
          <el-radio-group v-model="form.defaultState" size="small">
            <el-radio label="Y" border>开启</el-radio>
            <el-radio label="N" border>关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScoringMetrics, getScoringMetrics, delScoringMetrics, addScoringMetrics, updateScoringMetrics } from "@/api/system/scoringMetrics";

export default {
  name: "ScoringMetrics",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评估指标表格数据
      scoringMetricsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        defaultState: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scoringMetricsId: [
          { required: true, message: "评估指标id不能为空", trigger: "blur" }
        ],
        scoringMetricsName: [
          { required: true, message: "评估指标名称不能为空", trigger: "blur" }
        ],
        scoringMetricsYear: [
          { required: true, message: "评估指标年份不能为空", trigger: "blur" }
        ],
        scoringMetricsArray: [
          { validator: this.validateScoringMetricsList, trigger: "blur" }
        ],
        directReplyScore: [
          { required: true, message: "直接答辩分数线不能为空", trigger: "blur" }
        ],
        reviseReplyScore: [
          { required: true, message: "修改后答辩分数线不能为空", trigger: "blur" }
        ],
        reviseAuditScore: [
          { required: true, message: "修改后审核答辩分数线不能为空", trigger: "blur" }
        ],
        disagreeReplyScore: [
          { required: true, message: "不同意答辩不能为空", trigger: "blur" }
        ],
        defaultState: [
          { required: true, message: "默认开启的指标不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评估指标列表 */
    getList() {
      this.loading = true;
      listScoringMetrics(this.queryParams).then(response => {
        this.scoringMetricsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scoringMetricsId: undefined,
        scoringMetricsName: undefined,
        scoringMetricsYear: undefined,
        scoringMetrics: undefined,
        directReplyScore: undefined,
        reviseReplyScore: undefined,
        reviseAuditScore: undefined,
        disagreeReplyScore: undefined,
        defaultState: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scoringMetricsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评估指标";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const scoringMetricsId = row.scoringMetricsId || this.ids
      getScoringMetrics(scoringMetricsId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改评估指标";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.scoringMetricsId != null) {
            updateScoringMetrics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addScoringMetrics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scoringMetricsIds = row.scoringMetricsId || this.ids;
      this.$modal.confirm('是否确认删除评估指标编号为"' + scoringMetricsIds + '"的数据项？').then(() => {
        this.loading = true;
        return delScoringMetrics(scoringMetricsIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/scoringMetrics/export', {
        ...this.queryParams
      }, `scoringMetrics_${new Date().getTime()}.xlsx`)
    },
    //删除经验行
    handleDeleteExperience(index) {
      console.log(index);
      if (this.form.scoringMetricsArray.length == 1){
        this.$modal.msgError("最少保留一个评估指标");
        return
      }
      this.form.scoringMetricsArray.splice(index, 1)
    },
    //增加经验行
    handleAddExperienceline() {
      if (this.form.scoringMetricsArray == undefined) {
        this.form.scoringMetricsArray = new Array();
      }
      let obj = {};
      this.form.scoringMetricsArray.push(obj);
    },
    validateScoringMetricsList(rule, value, callback) {
      console.log(value)
      if (value == undefined || value.length === 0){
        callback(new Error("评估指标不能为空"));
      }else {
        for (let i = 0; i < value.length; i++) {
          const item = value[i];
          if (!item.metricsName || !item.metricsScore) {
            callback(new Error("评估指标必须填写完整"));
            return;
          }
        }
        callback();
      }
    },
  }
};
</script>
